var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

// import { $wuxRefresher } from '../../components/wux';
import { $wuxPrompt } from '../../components/wux';


var app = getApp();
var that;

let  pageInex = 1;

Page({
  data: { 
    user:null,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false,
    type:1,
    code:""
  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onPullDownRefresh:function(){
    pageInex = 1;
    that.setData({
      list : [],
      isLoadMore : false,
      isEnd : false,
      isEmpty:false
    });
    that.loadData();
  },
  onLoad: function (options) {
    that = this;
    

    if( !util.judge.isNull( options.type) ){
      that.setData({
        type: parseInt(options.type)
      });
    }

    if( !util.judge.isNull( options.code) ){
      that.setData({
        code: options.code
      });
    }


    if(that.data.type ==1){

        that.data._sdesc="我参加的竞赛";
        wx.setNavigationBarTitle( { title: that.data._sdesc });

    }else{
        that.data._sdesc="我管理的竞赛";
        wx.setNavigationBarTitle( { title: that.data._sdesc });

    }



    $wuxPrompt.init('msg1', {
      title: '暂时没有相关数据', 
    }).show()

 
    // that.refresher = new $wuxRefresher({
    //   onPulling() {
    //   },
    //   onRefresh() {
    //     pageInex = 1;
    //     this.start=true;
    //     that.setData({
    //       list : [],
    //       isLoadMore : false,
    //       isEnd : false,
    //       isEmpty:false
    //     });

    //     setTimeout(() => {
          
    //       that.loadData();
    //     },1000);
    //   }
    // })

  },
  // touchstart(e) {
  //   that.refresher.touchstart(e)
  // }, 
  // touchmove(e) {
  //   that.refresher.touchmove(e)
  // }, 
  // touchend(e) {
  //   that.refresher.touchend(e)
  // }, 
  onReady : function(){
    pageInex = 1;
    util.showLoading();
    
    app.getUserInfo()
    .then(res => {
      that.setData({
        user:res
      })
      that.loadData(); 
    });

  },
  onReachBottom : function(e,a){
      //加载更多
      if(!that.data.isEnd && !that.data.isLoadMore && !that.data.isEmpty){
          that.loadData();
      }
  },
  
  onForwardColumn : function(){
      wx.navigateTo({
        url : "../column/column"
      });
  },

  onPreImage : function(e){
      let src = e.currentTarget.dataset.imgSrc;
      Util.UI.preImage(src);
  },
  loadData: function(){

    that.setData({
      isLoadMore : true
    });

    var url = config.url_user_team;

    if(that.data.type == 2){
      url = config.url_user_team_admin; 
    }

    var params = {
      code:"",
      pageIndex: pageInex,
      pageSize: app.globalData.defaultPageSize
    }

    wxRequest.postRequest(url, params)
    .then(res=>{
      
      // if(that.refresher.start==true){
      //   that.refresher.start=false;
      //   that.refresher.events.emit(`scroll.refreshComplete`);
      // }

      if(pageInex==1 && res.length==0){
          that.setData({
            isLoadMore : false,
            isEnd : false,
            isEmpty :true,
          });
          return;
      }
      
      if(res.length > 0){
        pageInex++;

        for(var i in res){
            var now = new Date().getTime();
            var bd = util.str2date(res[i].bdate).getTime();
            var ed = util.str2date(res[i].edate).getTime();
            var ld = util.str2date(res[i].ldate).getTime();
            //判断
            var status = 0;
            var str="未开始";
            if(now  <  bd){
              status = 0;
               str="未开始";
            }else if( now >= bd && now <= ed )
            {
              status = 1;
              str="进行中";
            }
            else if( now > ed && now <= ld )
            {
              status = 2;
              str="评奖中";
            }
            else{
              status = 3;
              str="已结束";
            }

            res[i].str = str;
            res[i].status = status;
            res[i].bdate = util.date2str(res[i].bdate,"MM.dd");
            res[i].edate = util.date2str(res[i].edate,"MM.dd");
          }
      
        var data = that.data.list.concat(res);   
        that.setData({
            list : data,
            isLoadMore : false
        });
      }
      
      if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
        that.setData({
          isLoadMore : false,
          isEnd : true
        });
      }

    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
      wx.stopPullDownRefresh();
    });
  },
  onedit:function(e){
    var data = e.currentTarget.dataset;
    var  tid = data.id;

    if(that.data.type == 2){
      wx.navigateTo({
        url: '../team/edit?tid='+tid
      })
    }else{
      wx.navigateTo({
        url: '../team/main?tid='+tid
      })
    }
  },
  ondel:function(e){
    var data = e.currentTarget.dataset;
    var  tid = data.id;

  }

})


